package readin;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;

import fabrik.Relations;
import fabrik.Project;
import fabrik.Process;

/**
 * This class reads in the file and then searches it for certain commands.
 * 
 * @author Janet
 * @version 1.0
 */
public class ReadIn {
	/** splitting symbol. */
	final static String SEMIKOLON = "[;]";
	/** searching for the word project. */
	final static String PROJECT = "project";
	/** searching for the word task. */
	final static String TASK = "task";
	/** searching for the word aob. */
	final static String AOB = "aob";

	/**
	 * The constructor for this method,.
	 */
	public ReadIn() {

	}

	/**
	 * Initializes the project which impacts reading in the path and searching
	 * for the commands in the file.
	 * 
	 * @param path
	 *            ,path of the file
	 * @param project
	 *            we use in searchForCommands
	 */
	public static void initializeProjectWithFile(String path, Project project) {
		ArrayList<String> lines = readIn(path);
		searchForCommands(lines, project);
	}

	/**
	 * The file is read in here.
	 * 
	 * @param path
	 *            ,the path of the file
	 * @return list with the lines of the file
	 */
	private static ArrayList<String> readIn(String path) {

		ArrayList<String> list = new ArrayList<String>();
		try {
			// reads in the file
			BufferedReader reader = new BufferedReader(new FileReader(path));
			String line;
			// checks if the line is valid and then adds it to the list
			while ((line = reader.readLine()) != null) {
				list.add(line);
			}
			reader.close();
			// catches exception and prints out an error message
		} catch (IOException e) {
			System.out.println("Error! Couldn't read in the file. "
					+ e.toString());
		}
		// returns the list with the lines of the file
		return list;
	}

	/**
	 * Here we are looking for the commands project,task and aob. If these are
	 * found they get setted with the belonging values.
	 * 
	 * @param lines
	 *            contains every line of the files
	 * @param project
	 */
	private static void searchForCommands(final ArrayList<String> lines,
			Project project) {
		// counter for project,tasks and aobs
		int counterProject = 0;
		int task = 0;
		int aob = 0;
		for (int i = 0; i < lines.size(); i++) {
			// split the line at every ";" symbol
			String[] zeile = lines.get(i).split(SEMIKOLON);
			// if the length is equal four, it is valid

			if (zeile.length == 4) {
				// if the command is project, it sets a new project with its
				// values name,projectstart and an end
				if (zeile[0].equals(PROJECT)) {
					project.setName(zeile[3]);
					project.setProjectStart(Integer.parseInt(zeile[1]));
					project.setProjectEnd(Integer.parseInt(zeile[2]));
					// counts if just one project is created
					counterProject++;
				}
				// if the command is task, it sets a new process with the values
				// ID,name and period
				if (zeile[0].equals(TASK)
						&& Integer.parseInt(zeile[1]) == project
								.getProcessList().size()) {
					Process process = new Process(zeile[3],
							Integer.parseInt(zeile[1]),
							Integer.parseInt(zeile[2]), project);
					project.getProcessList().add(process);
					task++;
				}
				// if the command is aob, a relation will be set with
				// predecessor,successor and relation
				if (zeile[0].equals(AOB)) {
					Relations relations = new Relations(
							project.getProcessById(Integer.parseInt(zeile[1])),
							project.getProcessById(Integer.parseInt(zeile[2])),
							zeile[3]);
					project.getRelationList().add(relations);
					aob++;
				}
				// error message if a line is not equal 4
				if (zeile.length != 4) {
					System.out.print("Error! Invalid lines.");
					break;
				}
			}
			// if there was no project, less than 2 tasks or less than 1 aob,
			// print out this error
			if (counterProject != 1 && task < 2 && aob < 1) {
				System.out
						.print("Error! Not enough project,tasks or aobs have been set.");
				break;

			}
			// error message if there are more than one project command or less
			// than 2 tasks commands or less
			// than one aob command

		}
	}
}
